<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <meta content="yes" name="apple-mobile-web-app-capable" />
  <meta name="apple-mobile-web-app-status-bar-style" content="blank">
  <meta content="telephone=no" name="format-detection" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>天瑞商城</title>
  <link rel="stylesheet" href="./css/mreset.css">
  <link rel="stylesheet" href="./css/swiper-3.4.2.min.css">
  <link rel="stylesheet" href="./css/main.css">
  <script src="js/jquery-1.11.3.min.js"></script>
  <script src="js/flexible.js"></script>
  <script src="js/swiper-3.4.2.min.js"></script>
  <script src="js/bank.js"></script>
</head>
<body ontouchstart>
  <div class="wap">
    <div class="add-bank-box">
      <form action="">
        <ul>
          <li>
            <label for="">姓名</label><input type="text" placeholder="请输入姓名">
          </li>
          <li>
            <label for="">身份证号</label><input type="text" placeholder="请输入身份证号">
          </li>
          <li class="border10">
            <label for="">预留电话</label>
            <select name="" id="">
              <option value="">农业银行</option>
              <option value="">农业银行</option>
              <option value="">农业银行</option>
            </select>
          </li>
          <li class="border10">
            <label for="">银行卡号</label><input id="bank" minlength="19" maxlength="23" type="text" placeholder="请输入银行卡号" onkeyup="this.value =this.value.replace(/\s/g,'').replace(/[^\d]/g,'').replace(/(\d{4})(?=\d)/g,'$1 ');">
            <div id="banknoInfo" class="banknoInfo"></div>
          </li>

          <li class="border10">
            <label for="">预留电话</label><input type="text" placeholder="请输入银行预留电话">
          </li>
        </ul>
        <button class="" type="submit">保存</button>
      </form>
    </div>
  </div>
  <script>

    $('#bank').bind('input propertychange', function(){
      if($(this).val().length==19 || $(this).val().length==16){
        luhnCheck($(this).val());
      }
    })
    //银行卡号码检测
    function luhnCheck(bankno) {
      var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位（与luhn进行比较）
      var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
      var newArr = new Array();
      for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
        newArr.push(first15Num.substr(i, 1));
      }
      var arrJiShu = new Array(); //奇数位*2的积 <9
      var arrJiShu2 = new Array(); //奇数位*2的积 >9
      var arrOuShu = new Array(); //偶数位数组
      for (var j = 0; j < newArr.length; j++) {
        if ((j + 1) % 2 == 1) { //奇数位
          if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2);
          else arrJiShu2.push(parseInt(newArr[j]) * 2);
        } else //偶数位
          arrOuShu.push(newArr[j]);
      }

      var jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数
      var jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数
      for (var h = 0; h < arrJiShu2.length; h++) {
        jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
        jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
      }

      var sumJiShu = 0; //奇数位*2 < 9 的数组之和
      var sumOuShu = 0; //偶数位数组之和
      var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
      var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
      var sumTotal = 0;
      for (var m = 0; m < arrJiShu.length; m++) {
        sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
      }

      for (var n = 0; n < arrOuShu.length; n++) {
        sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
      }

      for (var p = 0; p < jishu_child1.length; p++) {
        sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
        sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
      }
      //计算总和
      sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);

      //计算luhn值
      var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
      var luhn = 10 - k;

      if (lastNum == luhn) {
        //var json =  bankCardAttribution(bankno);
        //$("#banknoInfo").html(json.bankName,json.cardTypeName);
        $("#banknoInfo").html("对");
        return true;
      } else {
        $("#banknoInfo").html("你输入的号码有误");
        return false;
      }
    }
  </script>
</body>
</html>